package com.odontosis.testeprincipal;

import java.awt.print.PrinterJob;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

public class GeradorImpressao {
	
	private static GeradorImpressao instance = new GeradorImpressao();
	private static PrintService printer = null;
	private static PrintRequestAttributeSet aset = null;
    
    private GeradorImpressao(){}
    
    public static GeradorImpressao getInstance() {
        return instance;
    }
    
    /**
     * Efetua impress?o do texto passado como par?metro.
     * @param texto
     */
    public static void detectaImpressora(String toPrint, String printerName) {
    	aset = new HashPrintRequestAttributeSet();
        aset.add(new JobName("Impressao", null));
        aset.add(MediaSizeName.ISO_A8);
        aset.add(new Copies(1));
       // aset.add(MediaTray.MAIN);
        
        
        for (PrintService p : PrinterJob.lookupPrintServices()) {
            if (p.getName().equalsIgnoreCase(printerName)) {
                printer = p;                
                break;
            }
        }
        //MediaPrintableArea printableArea = (MediaPrintableArea)printer.getSupportedAttributeValues(MediaPrintableArea.class, null, aset);
        
        try {
			print(toPrint);
		} catch (PrintException e) {
			e.printStackTrace();
		}
    }
    
    public synchronized static boolean print(String toPrint) throws PrintException {
    	
        try {
			DocPrintJob docPrint = printer.createPrintJob();
			PrintJobWatcher pjDone = new PrintJobWatcher(docPrint);
			InputStream stream = new ByteArrayInputStream(toPrint.getBytes());
			DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
			Doc doc = new SimpleDoc(stream, flavor, null);
			docPrint.print(doc, aset);
			pjDone.waitForDone();
			stream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return true;
        
    }
}
    class PrintJobWatcher {
        // true iff it is safe to close the print job's input stream
        boolean done = false;
    
        PrintJobWatcher(DocPrintJob job) {
            // Add a listener to the print job
            job.addPrintJobListener(new PrintJobAdapter() {
                public void printJobCanceled(PrintJobEvent pje) {
                    allDone();
                }
                public void printJobCompleted(PrintJobEvent pje) {
                    allDone();
                }
                public void printJobFailed(PrintJobEvent pje) {
                    allDone();
                }
                public void printJobNoMoreEvents(PrintJobEvent pje) {
                    allDone();
                }
                void allDone() {
                    synchronized (PrintJobWatcher.this) {
                        done = true;
                        PrintJobWatcher.this.notify();
                    }
                }
            });
        }
        public synchronized void waitForDone() {
            try {
                while (!done) {
                    wait();
                }
            } catch (InterruptedException e) {
            }
        }
    }


